package com.baitengsoft.patentservicemonitor.job.controller;


import com.baitengsoft.patentservicemonitor.common.annotation.SysLog;
import com.baitengsoft.patentservicemonitor.common.utils.Res;
import com.baitengsoft.patentservicemonitor.job.entity.ScheduleJobEntity;
import com.baitengsoft.patentservicemonitor.job.service.ScheduleJobService;
import com.baitengsoft.patentservicemonitor.job.utils.ScheduleUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 定时任务
 *
 * @author KennyGu
 * @email 89335272@qq.com
 * @date 2016年11月28日 下午2:16:40
 */
@RestController
@RequestMapping("/job/schedule")
@Api(value = "数据服务", description = "专利数据")
public class ScheduleJobController {
    @Autowired
    private Scheduler scheduler;

    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 定时任务信息
     */
    @RequestMapping(value = "/info/{jobId}", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务信息")
    public Res info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.queryObject(jobId);

        return Res.ok().put("schedule", schedule);
    }

    /**
     * 保存定时任务
     */
    @SysLog(value = "保存定时任务", username = "job操作者")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Res save(@RequestBody ScheduleJobEntity scheduleJob) {
        //验证暂时忽视--kenny

        scheduleJobService.save(scheduleJob);

        return Res.ok();
    }


    @SysLog(value = "保存定时任务", username = "job操作者")
    @RequestMapping(value = "/saveJob", method = RequestMethod.POST)
    public Res saveJob(long jobId) {
        ScheduleJobEntity scheduleJob = scheduleJobService.queryObject(jobId);
        if (scheduleJob != null) {
            ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
            return Res.ok();
        }
        return Res.error("创建定时任务失败");
    }


    /**
     * 修改定时任务
     */
    @SysLog(value = "修改定时任务", username = "job操作者")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "修改定时任务")
    public Res update(@RequestBody ScheduleJobEntity scheduleJob) {
        //验证暂时忽视--kenny

        scheduleJobService.update(scheduleJob);

        return Res.ok();
    }

    /**
     * 删除定时任务
     */
    @SysLog(value = "删除定时任务", username = "job操作者")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除定时任务")
    public Res delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return Res.ok();
    }

    /**
     * 立即执行任务(后台调用执行任务的接口)
     */
    @SysLog(value = "立即执行任务", username = "job操作者")
    @RequestMapping(value = "/run", method = RequestMethod.POST)
    @ApiOperation(value = "立即执行任务")
    public Res run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return Res.ok();
    }

    /**
     * 暂停定时任务
     */
    @SysLog(value = "暂停定时任务", username = "job操作者")
    @RequestMapping(value = "/pause", method = RequestMethod.POST)
    @ApiOperation(value = "暂停定时任务")
    public Res pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);
        return Res.ok();
    }

    /**
     * 恢复定时任务
     */
    @SysLog(value = "恢复定时任务", username = "job操作者")
    @RequestMapping(value = "/resume", method = RequestMethod.POST)
    @ApiOperation(value = "恢复定时任务")
    public Res resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return Res.ok();
    }


}
